package com.zhu.minio.controller;

import com.zhu.commons.utils.MinioUtilS;
import com.zhu.commons.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/admin/minio/file")
@Api(tags = "图片上传模块")
@CrossOrigin
@Slf4j
public class UploadController {
    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.bucketName}")
    private String bucketName;

    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public R upload(MultipartFile file) {
        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});
        String address=  endpoint+"/"+bucketName+"/"+upload.get(0);
        return R.ok().data("url",address);
    }
    @ApiOperation(value = "删除文件")
    @PostMapping("/delete")
    public R delete(@RequestParam("fileName") String fileName) {
        String newFileName= StringUtils.substringAfterLast(fileName,"babiq/");
        System.out.println(newFileName);
        if(minioUtilS.deleteFile(newFileName)){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }
}
